package com.monkeygroup.mygoplus.ecchat.ui.contact.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.monkeygroup.mygoplus.MyGoApplication;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.ecchat.ui.contact.entity.ContactMemberDTO;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2017/6/29.
 */
public class ChoosedMemberRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ContactMemberDTO> dataList;
    private Context context;
    private final LayoutInflater mInflater;

    public ChoosedMemberRecyclerAdapter(Context context, List<ContactMemberDTO> dataList) {
        this.context = context;
        this.dataList = dataList;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        return new MemberViewHolder(mInflater.inflate(R.layout.item_invite_group_member, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder == null)
            return;

        ((MemberViewHolder) holder).item_name.setText(dataList.get(position).getName());
        ImageView imageView = ((MemberViewHolder) holder).item_icon;
        if (dataList.get(position).getHeadUrl() != null && !dataList.get(position).getHeadUrl().equals("")) {
            ImageLoader.getInstance().displayImage(dataList.get(position).getHeadUrl(), imageView, MyGoApplication.getDefaultDisplayImgOption());
        } else {
            imageView.setImageResource(R.drawable.no_head_icon);
        }

        ((MemberViewHolder) holder).item_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataList.remove(position);
                notifyItemRemoved(position);
//                notifyDataSetChanged();
            }
        });

        ((MemberViewHolder) holder).layout_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (dataList != null) {
            return dataList.size();
        }

        return 0;
    }

    class MemberViewHolder extends RecyclerView.ViewHolder {
        LinearLayout layout_item;
        TextView item_name;
        ImageView item_icon;
        ImageView item_remove;

        public MemberViewHolder(View view) {
            super(view);
            layout_item = (LinearLayout) view.findViewById(R.id.layout_item);
            item_name = (TextView) view.findViewById(R.id.item_name);
            item_icon = (ImageView) view.findViewById(R.id.item_icon);
            item_remove = (ImageView) view.findViewById(R.id.item_remove);
        }
    }

}
